<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>zabbix监控php状态 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>zabbix监控php状态</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;通过nginx调用php-fpm来查询php的状态信息</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，在php的配置文件中添加一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /usr/<span class="built_in">local</span>/php/etc/php-fpm.conf</div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">pm.status_path = /phpfpmstatus</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后重启php-fpm</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在nginx配置文件中添加代码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">server </div><div class="line">&#123;  </div><div class="line">	listen localhost:80;  </div><div class="line">	server_name localhost;</div><div class="line">location /nginxstatus &#123;  </div><div class="line">	stub_status on;  </div><div class="line">	access_log off;  </div><div class="line">	allow 127.0.0.1;</div><div class="line">	allow 10.6.0.187;   </div><div class="line">	deny all;  </div><div class="line">	&#125;</div><div class="line">location ~ ^/(phpfpmstatus)$ &#123;</div><div class="line">        include fastcgi_params;</div><div class="line">        fastcgi_pass unix:/tmp/php-fcgi.sock;</div><div class="line">        fastcgi_param SCRIPT_FILENAME /usr/<span class="built_in">local</span>/nginx/html<span class="variable">$fastcgi_script_name</span>;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后重启nginx</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后使用<code>curl -s http://localhost/phpfpmstatus</code>查看是否能够获取到php状态信息</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后，在zabbix的脚本目录里面编辑执行脚本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"></div><div class="line"><span class="comment">#monitor php-fpm status from zabbix</span></div><div class="line"><span class="comment">#lincense:GPL</span></div><div class="line"><span class="comment">#mail:admin@huxianglin.cn</span></div><div class="line"><span class="comment">#date:2015.04.15</span></div><div class="line"></div><div class="line"><span class="built_in">source</span> /etc/bashrc &gt;/dev/null 2&gt;&amp;1</div><div class="line"><span class="built_in">source</span> /etc/profile &gt;/dev/null 2&gt;&amp;1</div><div class="line"></div><div class="line">LOG=/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpfpmstatus.log</div><div class="line">curl -s http://localhost/phpfpmstatus &gt;<span class="variable">$LOG</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">pool</span></span>()&#123;</div><div class="line">	awk <span class="string">'/pool/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">process_manager</span></span>()&#123;</div><div class="line">	awk <span class="string">'/process manager/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">start_since</span></span>()&#123;</div><div class="line">	awk <span class="string">'/start since:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">accepted_conn</span></span>()&#123;</div><div class="line">        awk <span class="string">'/accepted conn:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="title">listen_queue</span></span>()&#123;</div><div class="line">        awk <span class="string">'/^(listen queue:)/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">max_listen_queue</span></span>()&#123;</div><div class="line">        awk <span class="string">'/max listen queue:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">listen_queue_len</span></span>()&#123;</div><div class="line">        awk <span class="string">'/listen queue len:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">idle_processes</span></span>()&#123;</div><div class="line">        awk <span class="string">'/idle processes:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">active_processes</span></span>()&#123;</div><div class="line">        awk <span class="string">'/^(active processes:)/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">total_processes</span></span>()&#123;</div><div class="line">        awk <span class="string">'/total processes:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">max_active_processes</span></span>()&#123;</div><div class="line">        awk <span class="string">'/max active processes:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">max_children_reached</span></span>()&#123;</div><div class="line">        awk <span class="string">'/max children reached:/ &#123;print $NF&#125;'</span> <span class="variable">$LOG</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">pool)</div><div class="line">	pool</div><div class="line">	;;</div><div class="line">process_manager)</div><div class="line">	process_manager</div><div class="line">	;;</div><div class="line">start_since)</div><div class="line">	start_since</div><div class="line">	;;</div><div class="line">accepted_conn)</div><div class="line">	accepted_conn</div><div class="line">	;;</div><div class="line">listen_queue)</div><div class="line">	listen_queue</div><div class="line">	;;</div><div class="line">max_listen_queue)</div><div class="line">	max_listen_queue</div><div class="line">	;;</div><div class="line">listen_queue_len)</div><div class="line">	listen_queue_len</div><div class="line">	;;</div><div class="line">idle_processes)</div><div class="line">	idle_processes</div><div class="line">	;;</div><div class="line">active_processes)</div><div class="line">	active_processes</div><div class="line">	;;</div><div class="line">total_processes)</div><div class="line">	total_processes</div><div class="line">	;;</div><div class="line">max_active_processes)</div><div class="line">	max_active_processes</div><div class="line">	;;</div><div class="line">max_children_reached)</div><div class="line">	max_children_reached</div><div class="line">	;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;pool|process_manager|start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached&#125;"</span></div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后退出，修改权限为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">-rwxr-xr-x 1 zabbix zabbix 1770 4月  15 14:50 phpstatus.sh</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑zabbix_agentd.conf文件，在最后添加上下面这段代码，并重启zabbix_agentd服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#to monitor php-fpmstatus</span></div><div class="line">UserParameter=phpfpm.status.pool,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh pool</div><div class="line">UserParameter=phpfpm.status.process.manager,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh process_manager</div><div class="line">UserParameter=phpfpm.status.start.since,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh start_since</div><div class="line">UserParameter=phpfpm.status.accepted.conn,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh accepted_conn</div><div class="line">UserParameter=phpfpm.status.listen.queue,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh listen_queue</div><div class="line">UserParameter=phpfpm.status.max.listen.queue,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh max_listen_queue</div><div class="line">UserParameter=phpfpm.status.listen.queue.len,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh listen_queue_len</div><div class="line">UserParameter=phpfpm.status.idle.processes,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh idle_processes</div><div class="line">UserParameter=phpfpm.status.active.processes,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh active_processes</div><div class="line">UserParameter=phpfpm.status.total.processes,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh total_processes</div><div class="line">UserParameter=phpfpm.status.max.active.processes,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh max_active_processes</div><div class="line">UserParameter=phpfpm.status.max.children.reached,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/phpstatus.sh max_children_reached</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后就可以在web端配置item监控php状态了</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E7%9B%91%E6%8E%A7php%E7%8A%B6%E6%80%81/01.jpeg?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Zabbix/16. zabbix监控php状态/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
